import CryptoJS from 'crypto-js'

// const iv = '0123456789abcdef'

export function encrypt (cipher,key = 'com.inspur.dgov@') {
	if(cipher === '' || cipher === null || cipher === undefined  ) return false;
	let data,encrypted = '';
	if (typeof cipher === 'object') 
		data = JSON.stringify(cipher);
	else
		data = String(cipher)
    key = CryptoJS.enc.Utf8.parse(key)
    var iv = key
	encrypted = CryptoJS.AES.encrypt(data, key, {
		iv: iv,
		mode: CryptoJS.mode.CBC,
		padding: CryptoJS.pad.Pkcs7
	});
	return encodeURIComponent(encrypted.toString());
}

export function decrypt (cipher,key = 'com.inspur.dgov@') {
	if(cipher === '' || cipher === null || cipher === undefined  ) return false;
    let data = decodeURIComponent(cipher);
    key = CryptoJS.enc.Utf8.parse(key)
    var iv = key
	const decrypted = CryptoJS.AES.decrypt(data, key, {
		iv: iv,
		mode: CryptoJS.mode.CBC,
		padding: CryptoJS.pad.Pkcs7
	});
	return decrypted.toString(CryptoJS.enc.Utf8);
}

export function chartob (str) {
	return window.btoa(unescape(encodeURIComponent(str)))
}

export function btochar (str) {
	return decodeURIComponent(escape(window.atob(str)))
}